/**
 * 
 */
package com.maywide.jdp.system.bean;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.maywide.jdp.core.exception.AppException;
import com.maywide.jdp.core.vo.Page;
import com.maywide.jdp.dao.Dao;
import com.maywide.jdp.dao.PageQueryTemplate;
import com.maywide.jdp.dao.QueryTemplate;
import com.maywide.jdp.system.entities.Rule;

/**
 * 规则管理Bean
 * 
 * @author luoyang
 * @version 1.0
 * @since JDK1.6
 */
@Component("ruleBean")
@Transactional(propagation=Propagation.REQUIRED)
public class RuleBeanImpl implements RuleBean {
	/**
	 * 基础DAO
	 */
	private Dao dao = null;

	/**
	 * @param dao the dao to set
	 */
	@Autowired
	public void setDao(Dao dao) {
		this.dao = dao;
	}

	@Override
	public void insert(Object obj) throws Exception {
		dao.insert(obj);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void remove(Class clazz,Serializable id) throws Exception {
		Object obj = dao.find(clazz, id);
		dao.remove(obj);
	}

	@Override
	public void update(Object obj) throws Exception {
		dao.update(obj);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	@Override
	public Object load(Serializable id) throws Exception {
		Rule button = dao.find(Rule.class, id);
		return button;
	}

	@Override
	public void lock(String id) throws Exception {
		String jql = "update Button as o set o.active = false where o.id = :id";
		Query query = dao.createQuery(jql);
		query.setParameter("id", id);
		query.executeUpdate();
	}

	@Override
	public void lockAll(String[] ids) throws Exception {
		String hql = "update Rule as o set o.active = false where o.id in (:ids)";
		Map<String, Object> conditions = new HashMap<String,Object>();
		conditions.put("ids", ids);
		dao.executeUpdate(hql,conditions);
	}

	@Override
	public void unlock(String id) throws Exception {
		String jql = "update Rule as o set o.active = true where o.id = :id";
		Query query = dao.createQuery(jql);
		query.setParameter("id", id);
		query.executeUpdate();
	}

	@Override
	public void unlockAll(String[] ids) throws Exception {
		String hql = "update Rule as o set o.active = true where o.id in (:ids)";
		Map<String, Object> conditions = new HashMap<String,Object>();
		conditions.put("ids", ids);
		dao.executeUpdate(hql,conditions);
	}

	@Override
	public void move(String id, String targetID) throws Exception {
		String jql = "update Rule as o set o.module.id = :targetID where o.id = :id";
		Query query = dao.createQuery(jql);
		int row = 0;
		query.setParameter("id", id);
		query.setParameter("targetID", targetID);
		row = query.executeUpdate();
		if(row <= 0)
			throw new AppException("未发生移动操作!");
	}

	@Override
	public void move(List<String> ids, String targetID) throws Exception {
		int row = 0;
		String hql = "update Rule as o set o.module.id = :targetID where o.id in (:ids)";
		Map<String, Object> conditions = new HashMap<String,Object>();
		conditions.put("ids", ids);
		conditions.put("targetID", targetID);
		row = dao.executeUpdate(hql, conditions);
		if(row <= 0)
			throw new AppException("未发生移动操作!");
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	@Override
	public Page page(int first, int limit, String sort, String dir,
			Map<String, Object> conditions,Map<String, Object> parameters) throws Exception {
		Page page = null;

		String queryString = "from Rule as o";
		String countQueryString = "select count(o.id) from Rule as o";
		if(conditions == null)
			conditions = new HashMap<String,Object>();
		if(!conditions.containsKey("moduleID"))
			conditions.put("noModuleID", null);
		PageQueryTemplate template = new PageQueryTemplate(queryString,
				countQueryString, first, limit, sort, dir);
		page = dao.executePageQuery(template, conditions, null);

		return page;
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	@Override
	public List<Rule> list(String sort, String dir, Map<String, Object> conditions,
			Map<String, Object> parameters)
			throws Exception {
		List<Rule> list = null;
		String queryString = "select distinct o from Rule as o inner join o.functions as f";
		QueryTemplate template = new QueryTemplate(queryString,sort,dir);
		list = dao.executeListQuery(template, conditions, parameters);
		if(list != null && !list.isEmpty()) {
			for(Rule rule : list) {
				rule.getPath();
			}
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	@Override
	public List<Rule> findAllActive() throws Exception {
		Query query = dao.createNamedQuery("com.maywide.jdp.system.entities.Rule.findAllActive");
		return query.getResultList();
	}

}
